package com.projectName.www.dao;

import com.projectName.www.po.orders;
import com.projectName.www.util.String_util;

import java.sql.ResultSet;
import java.sql.SQLException;

public class Y_J_intoshop_dao {
    //填充店铺的菜品
    public static ResultSet filldishes(String name) throws Exception {
        String sql="select * from dishes where shopname =?";
        ResultSet resultSet = BaseDao.executeQuery(sql, name);
        return resultSet;

    }
    //展示优惠券——>可以使用的
    public  static  ResultSet fillcoupons(String name) throws Exception {
        String sql="select * from coupons where shopname = ?";
       ResultSet r= BaseDao.executeQuery(sql,name);
       return  r;
    }
    //领取优惠券后更新
    public static boolean getcoupon(String name) throws Exception {
        String sql="update coupons set num=num-1 where shopname =? and num>=1";
        int i=BaseDao.executeUpdata(sql,name);
        int j;
        if(i>0) {
            String sql1 = "update coupons set usernum=usernum+1  where shopname= ?";
             j = BaseDao.executeUpdata(sql1, name);
        }
        else j=0;
        if(j>0)return true;
        else return false;
    }
    //给优惠券注明客户id
    public static  boolean username(String username,String shopname) throws Exception {
        String sql="update coupons set username =? where shopname=?";
       int i= BaseDao.executeUpdata(sql,username,shopname);
        if(i>0)return true;
        else return false;
    }
    //客户评价
    public static boolean makemessage(String message,String shopname,String buyername) throws Exception {
        if(String_util.isEmpty(message))return false;
        String spl="insert into message (buyers,message,shopname) values(?,?,?)";

        int i=BaseDao.executeUpdata(spl,buyername,message,shopname);
        if(i>0)return true;
        else return false;
    }
    //遍历优惠券——>寻找未过期
    public static ResultSet coupon (String username,String shopname) throws Exception {
        String sql ="select * from coupons where username = ? and shopname =? and usernum >=1";
        ResultSet resultSet = BaseDao.executeQuery(sql, username,shopname);
        return resultSet;
    }
    //使用优惠券后更新
    public static boolean updatecoupon(String username,String shopname,String finaltime) throws Exception {
        String sql="update coupons set usernum=usernum-1 where username = ? and shopname =? and usernum >=1 " +
                "and finaltime = ?";
        int i = BaseDao.executeUpdata(sql, username, shopname,finaltime);
        if(i>0)return true;
        else return false;
    }
    //下单&&加入购物车
    public static boolean getorder(orders r) throws Exception {

        String sql ="insert into orders (Buyers,business,dishename,money,bool_buyer,num,bool_shop)  " +
                "values(?,?,?,?,?,?,?)";
       int j=1;
        int i= BaseDao.executeUpdata(sql,r.getBuyers(),r.getBusiness(),r.getDishename(),r.getMoney(),r.getBool_buyer(),r.getNum(),r.getBool_shop());
       if(r.getBool_shop()==1) {
           try{
           String Sql = " update user set money =money -? where id =0 and name =?";
            j = BaseDao.executeUpdata(Sql, r.getMoney(), r.getBuyers());
       }catch (SQLException e)
           {
               j=0;
           }
       }

       if(i>0&&j>0)return true;
       return false;
    }
    //更新账户金额
    public  static  ResultSet upmoney( String name ) throws Exception {
        String sql="select * from user where name = ? and id =0";
        ResultSet resultSet = BaseDao.executeQuery(sql, name);
        return  resultSet;
    }

}
